﻿using Model.Core;
using Models;
using SIE.SCADA.Common.Log;
using SIE.SCADA.SqlSugar;

using SIE.SCADA.ZemtDemo.Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIE.SCADA.ZemtDemo.ViewModel
{
    public class AddrecdViewModel: NotifyPropertyChanged
    {
        string _DisqueNo;
        private Trackin trackin = new Trackin();
        private State _workState;
        private smdc_disid_t _woInfo;
        //private List<scada_process> _SCADA_Processes;
        PgSqlAccess pgsql = new PgSqlAccess();

        public AddrecdViewModel(string disqueNo)
        {
            _DisqueNo = disqueNo;
            Message = new Message();
        }

        public void Clear()
        {
            _woInfo = null;
            ScanMsg = string.Empty;
            MatSn = string.Empty;
            MatNO = string.Empty;
            OrderSn = string.Empty;
            DisNO = string.Empty;
            SCADA_Jobsteps?.Clear();
            Retry_Jobsteprecords?.Clear();
            Retry_Jobsteprecord = new scada_jobstep_record();
            _workState = State.Wait;
        }

        public void KeyInput()
        {
            Read_ScanNO(ScanMsg);
        }

        /// <summary>
        /// 读单处理
        /// </summary>
        /// <param name="no"></param>
        public OrderMsg Read_ScanNO(string no)
        {
            OrderMsg orderMsg = new OrderMsg();
            CheckBarcode barcodeInfo = Trackin.GetListBarcodeInfo(no);
            switch (_workState)
            {
                case State.Wait:
                case State.Finish:
                default:
                    ScanMsg = string.IsNullOrEmpty(barcodeInfo.SerialNumber) ? no : barcodeInfo.SerialNumber;
                        if (_woInfo == null )
                        {
                            if (GetWoInfo(ScanMsg))
                            {
                                orderMsg.OrderNo = _woInfo.disidno;
                            }
                            else
                            {
                                orderMsg.Error = true;
                                orderMsg.ErrorMsg = "获取工单信息失败";
                            }
                        }
                        else
                        {
                            var process = GetProcessBySn(ScanMsg, _woInfo?.disidno);
                            if (process.Any())
                            {
                                OrderSn = ScanMsg;
                                orderMsg.Sn = OrderSn;
                                orderMsg.ProcessNo = _DisqueNo;
                                orderMsg.Cur_Process = CurProcess;
                            }
                            else
                            {
                                orderMsg.Error = true;
                                orderMsg.ErrorMsg = "获取工序信息失败";
                            }
                        }
                    break;
            }
            return orderMsg;
        }

        private List<scada_jobstep_record> GetProcessBySn(string sn,string disidNo)
        {
            var res = new List<scada_jobstep_record>();
            var proce = QueryData.Get_ProceRecord(sn, disidNo, _DisqueNo);
            if(proce != null)
                CurProcess = proce;
            SCADA_Jobsteps.Clear();
             res = QueryData.Get_StepRecord(sn, disidNo, _DisqueNo);
            foreach (var item in res)
            {
                SCADA_Jobsteps.Add(item);
            }
            return res;
        }

        public bool GetRetryRecord()
        {
            if (CurProcess.state_int == (int)State.Finish)
            {
                Retry_Jobsteprecords = SCADA_Jobsteps.ToList();
                Retry_Jobsteprecord = CurItem;
                return true;
            }
            return false;
        }

        public bool GetRetryProce()
        {
            if (CurProcess.state_int == (int)State.Finish)
            {
                RetryProceTag = true;
            }
            //RetryProceTag = false;
            return RetryProceTag;
        }

        public void Dispose()
        {
            Retry_Jobsteprecords?.Clear();
        }

        /// <summary>
        /// 获取工单信息
        /// </summary>
        /// <param name="wo">工单号</param>
        public bool GetWoInfo(string wo)
        {
            try
            {
                var woInfo = QueryData.GetWoInfo(wo);
                if (woInfo == null)
                {
                    RefreshMessage("没找到该工单", MessageState.Error);
                    return false;
                }
                else
                {
                    if (woInfo.state_int == (int)State.Finish)
                    {
                        RefreshMessage("该工单已完成！", MessageState.Error);
                        return false;
                    }
                    _woInfo = woInfo;
                    DisidNo = _woInfo?.disidno;
                    RefreshMessage("请选择工序或扫描SN", MessageState.Sucess);
                }
                DisNO = woInfo.disidno;
                MatNO = woInfo.matid;
                ProcessNO = _DisqueNo;
            }
            catch (Exception ex)
            {
                Logger.Warn($"查询订单出错{ex.Message}");
            }
            return false;
        }

        /// <summary>
        /// 刷新信息
        /// </summary>
        /// <param name="content"></param>
        /// <param name="state"></param>
        private void RefreshMessage(string content, MessageState state)
        {
            Message.Content = content;
            Message.State = (int)state;
        }

        public string DisidNo { get; private set; }

        public string Matid { get; set; }
        /// <summary>
        /// 当前工单的MBom
        /// </summary>
        public string MBom { get; set; }

        public string MatSn
        {
            get { return _MatSn; }
            set
            {
                if (value != _MatSn)
                {
                    _MatSn = value;
                    OnPropertyChanged(nameof(MatSn));
                }
            }
        }
        private string _MatSn;

        public string DisNO
        {
            get { return _DisNO; }
            set
            {
                if (value != _DisNO)
                {
                    _DisNO = value;
                    OnPropertyChanged(nameof(DisNO));
                }
            }
        }
        private string _DisNO;

        public string MatNO
        {
            get { return _MatNO; }
            set
            {
                if (value != _MatNO)
                {
                    _MatNO = value;
                    OnPropertyChanged(nameof(MatNO));
                }
            }
        }
        private string _MatNO;

        public string ProcessNO
        {
            get { return _ProcessNO; }
            set
            {
                if (value != _ProcessNO)
                {
                    _ProcessNO = value;
                    OnPropertyChanged(nameof(ProcessNO));
                }
            }
        }
        private string _ProcessNO;

        public string OrderSn
        {
            get { return _OrderSn; }
            set
            {
                if (value != _OrderSn)
                {
                    _OrderSn = value;
                    OnPropertyChanged(nameof(OrderSn));
                }
            }
        }
        private string _OrderSn;

        public Message Message { get; set; }
        /// <summary>
        /// 重打的工序信息
        /// </summary>
        public List<scada_jobstep_record> Retry_Jobsteprecords { get; set; }
        /// <summary>
        /// 重打步
        /// </summary>
        public scada_jobstep_record Retry_Jobsteprecord { get; set; }
        /// <summary>
        /// 当前项
        /// </summary>
        public scada_jobstep_record CurItem { get; set; }
        private ObservableCollection<scada_jobstep_record> _SCADA_jobsteps = new ObservableCollection<scada_jobstep_record>();
        /// <summary>
        /// 工序信息
        /// </summary>
        public ObservableCollection<scada_jobstep_record> SCADA_Jobsteps
        {
            get { return _SCADA_jobsteps; }
            set
            {
                _SCADA_jobsteps = value;
                if (_SCADA_jobsteps != value)
                {
                    OnPropertyChanged(nameof(SCADA_Jobsteps));
                }
            }
        }
        /// <summary>
        /// 显示扫码信息
        /// </summary>
        public string ScanMsg
        {
            get { return _scanMsg; }
            set
            {
                if (value != _scanMsg)
                {
                    _scanMsg = value;
                    OnPropertyChanged(nameof(ScanMsg));
                }
            }
        }
        private string _scanMsg;

        public scada_process_record CurProcess { get; set; }

        public bool RetryProceTag { get; set; }
    }
}
